home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / BOCOLE.PAK / MOVEWIN.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  14KB  |  381 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectComponents
  3. // Copyright (c) 1991, 1996 by Borland International, All Rights Reserved
  4. //
  5. // $Revision:   2.3  $
  6. //
  7. // MoveWin.cpp -- An implementation of a floating border when
  8. //                                moving/resizing a window
  9. //----------------------------------------------------------------------------
  10. #ifndef MOVEWIN_H
  11. #include "movewin.h"
  12. #endif
  13.  
  14. #include <mem.h>
  15.  
  16. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  17. ////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
  18. // 
  19. //
  20. //    Purpose:
  21. //
  22. //  Parameters:
  23. //
  24. //  Return value:
  25. //
  26. //
  27. GrayGhostRect::GrayGhostRect(RECT& WinSize, UINT wHitTestCode, UINT wLineSize) : 
  28.                                                                         wHitCode(wHitTestCode),
  29.                                                                         wThickness(wLineSize),
  30.                                                                         hBrush(0), hBmp(0)
  31. {
  32.     // initialize data members
  33.     CopyRect ((LPRECT)&GrayRect, (LPRECT)&WinSize);
  34.  
  35.     // initialize pattern information
  36.     WORD pattern[] = {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};
  37.     FramePattern (pattern, 16);
  38. }
  39.  
  40. GrayGhostRect::~GrayGhostRect()
  41. {
  42.     DeleteObject (hBrush);
  43.     DeleteObject (hBmp);
  44. }
  45.  
  46. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  47. ////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
  48. // GrayGhostRect::FramePattern
  49. //
  50. //    Purpose:
  51. //        create brush and bitmap to be used in drawing to the screen
  52. //
  53. //  Parameters:
  54. //        const WORD*                            pattern array 
  55. //        int                                            array size
  56. //
  57. //  Return value:
  58. //        HBRUSH                                    created brush or null if the function fails
  59. //
  60. HBRUSH GrayGhostRect::FramePattern (const WORD* newPattern, int nSize)
  61. {
  62.     // create bitmap for the brush. It must be 8x8. We use monochrome bitmap.
  63.     memcpy (wGrayBmp, newPattern, nSize);
  64.     if (hBmp)
  65.         DeleteObject (hBmp);
  66.     hBmp = CreateBitmap (8, 8, 1, 1, wGrayBmp);
  67.   
  68.     if (hBmp)  {
  69.         // create and store our brush
  70.         if (hBrush)
  71.             DeleteObject (hBrush);
  72.         hBrush = CreatePatternBrush (hBmp);
  73.     }
  74.  
  75.   return hBrush;
  76. }
  77.  
  78. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  79. ////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
  80. // GrayGhostRect::MoveFrame
  81. //
  82. //    Purpose:
  83. //        main function for simulating moving or resizing
  84. //
  85. //  Parameters:
  86. //        POINT                                        starting point
  87. //        BOOL                                        indicates move or resize
  88. //
  89. void GrayGhostRect::MoveFrame(POINT &MousePos, BOOL bOnMove)
  90. {
  91.         HBRUSH  hbrOld;
  92.         HDC hDC; 
  93.  
  94.     if (hBrush)  {
  95.         hDC = ::GetDC (NULL); // get screen DC
  96.         hbrOld = SelectObject(hDC, hBrush); // change brush
  97.     }
  98.   else
  99.       return; // no brush, abort operation. Here an exception would be much better
  100.         
  101.     if (bOnMove) {
  102.         RECT newRectFrame;
  103.  
  104.         switch (wHitCode) {
  105.             // resizing check window size; we don't want resizing
  106.       // to invert the window or nuke the window
  107.             case HTTOPLEFT:
  108.                 if (MousePos.y > GrayRect.bottom - MINIMUM_SIZE)
  109.                     MousePos.y = GrayRect.bottom - MINIMUM_SIZE;
  110.                 if (MousePos.x > GrayRect.right - MINIMUM_SIZE)
  111.                     MousePos.x = GrayRect.right - MINIMUM_SIZE;
  112.                 SetRect ((LPRECT)&newRectFrame, 
  113.                                                             GrayRect.left + MousePos.x - StartMove.x, 
  114.                                                             GrayRect.top + MousePos.y - StartMove.y,
  115.                                                             GrayRect.right, GrayRect.bottom);
  116.             break;
  117.             case HTLEFT:
  118.                 if (MousePos.x > GrayRect.right - MINIMUM_SIZE)
  119.                     MousePos.x = GrayRect.right - MINIMUM_SIZE;
  120.                 SetRect ((LPRECT)&newRectFrame, MousePos.x, GrayRect.top,
  121.                                                             GrayRect.right, GrayRect.bottom);
  122.             break;
  123.             case HTBOTTOMLEFT:
  124.                 if (MousePos.y < GrayRect.top + MINIMUM_SIZE)
  125.                     MousePos.y = GrayRect.top + MINIMUM_SIZE;
  126.                 if (MousePos.x > GrayRect.right - MINIMUM_SIZE)
  127.                     MousePos.x = GrayRect.right - MINIMUM_SIZE;
  128.                 SetRect ((LPRECT)&newRectFrame, 
  129.                                                             GrayRect.left + MousePos.x - StartMove.x, 
  130.                                                             GrayRect.top,    GrayRect.right, 
  131.                                                             GrayRect.bottom + MousePos.y - StartMove.y);
  132.             break;
  133.             case HTTOP:
  134.                 if (MousePos.y > GrayRect.bottom - MINIMUM_SIZE)
  135.                     MousePos.y = GrayRect.bottom - MINIMUM_SIZE;
  136.                 SetRect ((LPRECT)&newRectFrame, GrayRect.left, MousePos.y,
  137.                                                             GrayRect.right, GrayRect.bottom);
  138.             break;
  139.             case HTBOTTOM:
  140.                 if (MousePos.y < GrayRect.top + MINIMUM_SIZE)
  141.                     MousePos.y = GrayRect.top + MINIMUM_SIZE;
  142.                 SetRect ((LPRECT)&newRectFrame, GrayRect.left, GrayRect.top,
  143.                                                             GrayRect.right, MousePos.y);
  144.             break;
  145.             case HTTOPRIGHT:
  146.                 if (MousePos.y > GrayRect.bottom - MINIMUM_SIZE)
  147.                     MousePos.y = GrayRect.bottom - MINIMUM_SIZE;
  148.                 if (MousePos.x < GrayRect.left + MINIMUM_SIZE)
  149.                     MousePos.x = GrayRect.left + MINIMUM_SIZE;
  150.                 SetRect ((LPRECT)&newRectFrame, GrayRect.left, 
  151.                                                             GrayRect.top + MousePos.y - StartMove.y,
  152.                                                             GrayRect.right + MousePos.x - StartMove.x, 
  153.                                                             GrayRect.bottom);
  154.             break;
  155.             case HTRIGHT:
  156.                 if (MousePos.x < GrayRect.left + MINIMUM_SIZE)
  157.                     MousePos.x = GrayRect.left + MINIMUM_SIZE;
  158.                 SetRect ((LPRECT)&newRectFrame, GrayRect.left, GrayRect.top,
  159.                                                             MousePos.x, GrayRect.bottom);
  160.             break;
  161.             case HTBOTTOMRIGHT:
  162.                 if (MousePos.y < GrayRect.top + MINIMUM_SIZE)
  163.                     MousePos.y = GrayRect.top + MINIMUM_SIZE;
  164.                 if (MousePos.x < GrayRect.left + MINIMUM_SIZE)
  165.                     MousePos.x = GrayRect.left + MINIMUM_SIZE;
  166.                 SetRect ((LPRECT)&newRectFrame, GrayRect.left, GrayRect.top,
  167.                                                                 GrayRect.right - StartMove.x + MousePos.x, 
  168.                                                                 GrayRect.bottom - StartMove.y + MousePos.y);
  169.             break;
  170.             // just move, no resizing involved
  171.             default:
  172.                 // adjust offset since moving is upon top/left position
  173.                 CopyRect ((LPRECT)&newRectFrame, (LPRECT)&GrayRect);
  174.                 OffsetRect ((LPRECT)&newRectFrame, MousePos.x - StartMove.x,
  175.                                                             MousePos.y - StartMove.y);
  176.                 //StartMove.x = MousePos.x;
  177.                 //StartMove.y = MousePos.y;
  178.         }
  179.  
  180.         StartMove.x = MousePos.x;
  181.         StartMove.y = MousePos.y;
  182.  
  183.         RECT TempFrameSubRect1, TempFrameSubRect2;
  184.         //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  185.         // get left frame sub rectangle of old position
  186.         TempFrameSubRect1.left = GrayRect.left;
  187.         TempFrameSubRect1.top = GrayRect.top;
  188.         TempFrameSubRect1.right = GrayRect.left + wThickness;
  189.         TempFrameSubRect1.bottom = GrayRect.bottom;
  190.         // get left frame sub rectangle of new position
  191.         TempFrameSubRect2.left = newRectFrame.left;
  192.         TempFrameSubRect2.top = newRectFrame.top;
  193.         TempFrameSubRect2.right = newRectFrame.left + wThickness;
  194.         TempFrameSubRect2.bottom = newRectFrame.bottom;
  195.         // do drawing
  196.         UpdateRect (hDC, TempFrameSubRect1, TempFrameSubRect2);
  197.  
  198.         //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  199.         // get right frame sub rectangle of old position
  200.         TempFrameSubRect1.left = GrayRect.right - wThickness;
  201.         TempFrameSubRect1.right = GrayRect.right;
  202.         // get right frame sub rectangle of new position
  203.         TempFrameSubRect2.left = newRectFrame.right - wThickness;
  204.         TempFrameSubRect2.right = newRectFrame.right;
  205.         // do drawing
  206.         UpdateRect (hDC, TempFrameSubRect1, TempFrameSubRect2);
  207.  
  208.         //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  209.         // get top frame sub rectangle of old position
  210.         TempFrameSubRect1.left = GrayRect.left + wThickness;
  211.         TempFrameSubRect1.right -= wThickness;
  212.         TempFrameSubRect1.bottom = GrayRect.top + wThickness;
  213.         // get top frame sub rectangle of new position
  214.         TempFrameSubRect2.left = newRectFrame.left + wThickness;
  215.         TempFrameSubRect2.right -= wThickness;
  216.         TempFrameSubRect2.bottom = newRectFrame.top + wThickness;
  217.         // do drawing
  218.         UpdateRect (hDC, TempFrameSubRect1, TempFrameSubRect2);
  219.  
  220.         //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  221.         // get bottom frame sub rectangle of old position
  222.         TempFrameSubRect1.top = GrayRect.bottom - wThickness;
  223.         TempFrameSubRect1.bottom = GrayRect.bottom;
  224.         // get bottom frame sub rectangle of new position
  225.         TempFrameSubRect2.top = newRectFrame.bottom - wThickness;
  226.         TempFrameSubRect2.bottom = newRectFrame.bottom;
  227.         // do drawing
  228.         UpdateRect (hDC, TempFrameSubRect1, TempFrameSubRect2);
  229.  
  230.  
  231.         //XORFrame(hDC); //old solution
  232.         CopyRect ((LPRECT)&GrayRect, (LPRECT)&newRectFrame);
  233.         //XORFrame(hDC); // old solution
  234.     } // end if (bOnMove)
  235.     else
  236.         // this is the first or the last drawing
  237.         XORFrame(hDC);
  238.  
  239.     SelectObject(hDC, hbrOld); // select back old brush
  240.     ReleaseDC (NULL, hDC); // release DC
  241. }
  242.  
  243. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  244. ////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
  245. // GrayGhostRect::XORFrame
  246. //
  247. //    Purpose:
  248. //        XOR the current rect with the screen
  249. //
  250. //  Parameters:
  251. //        HDC                                            DC to xor our rect
  252. //
  253. void GrayGhostRect::XORFrame (HDC hDC)
  254. {
  255.     PatBlt(hDC, GrayRect.left, GrayRect.top,
  256.                                                 GrayRect.right - GrayRect.left,
  257.                                                 wThickness, PATINVERT);
  258.     PatBlt(hDC, GrayRect.left, GrayRect.top + wThickness, wThickness,
  259.                                                 GrayRect.bottom - GrayRect.top  - wThickness,
  260.                                                 PATINVERT);
  261.     PatBlt(hDC, GrayRect.right - wThickness, GrayRect.top + wThickness,
  262.                                                 wThickness,
  263.                                                 GrayRect.bottom - GrayRect.top  - wThickness,
  264.                                                 PATINVERT);
  265.     PatBlt(hDC, GrayRect.left + wThickness, GrayRect.bottom - wThickness,
  266.                                                 GrayRect.right - wThickness - GrayRect.left - wThickness,
  267.                                                 wThickness, PATINVERT);
  268. }
  269.  
  270. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  271. ////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
  272. // GrayGhostRect::SetStartMove
  273. //
  274. //    Purpose:
  275. //        set data member StartMove
  276. //
  277. void GrayGhostRect::SetStartMove (const POINT& mousePoint)
  278. {
  279.     StartMove.x = mousePoint.x;
  280.     StartMove.y = mousePoint.y;
  281. }
  282.  
  283. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  284. ////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
  285. // GrayGhostRect::UpperCorner
  286. //
  287. //    Purpose:
  288. //        set data mousePoint to the upper left corner of the window rect
  289. //
  290. void GrayGhostRect::UpperCorner (POINT& TopLeft) const
  291. {
  292.     TopLeft.x = GrayRect.left;
  293.     TopLeft.y = GrayRect.top;
  294. }
  295.  
  296. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  297. ////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
  298. // GrayGhostRect::Size
  299. //
  300. //    Purpose:
  301. //        return window size
  302. //
  303. void GrayGhostRect::Size (POINT& WidthEight) const
  304. {
  305.     WidthEight.x = GrayRect.right - GrayRect.left;
  306.     WidthEight.y = GrayRect.bottom - GrayRect.top;
  307. }
  308.  
  309. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  310. ////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
  311. // GrayGhostRect::UpdateRect
  312. //
  313. //    Purpose:
  314. //        this function draws the rect on the screen. If there are overlapped
  315. //        areas only draws non overlapped parts
  316. //
  317. //  Parameters:
  318. //        HDC                                            Device context to draw on
  319. //        RECT&                                        old rectangle position
  320. //        RECT&                                        new rectangle position
  321. //
  322. //
  323. void GrayGhostRect::UpdateRect (HDC hDC, RECT& oldRect, RECT& newRect)
  324. {
  325.     RECT Intersection;
  326.     if (IntersectRect ((LPRECT)&Intersection, (LPRECT)&oldRect, (LPRECT)&newRect)) {
  327.         if (oldRect.top >= newRect.top || oldRect.bottom >= newRect.bottom) {
  328.             if (oldRect.left <= newRect.left || oldRect.right <= newRect.right) {
  329.                 PatBlt(hDC, newRect.left, newRect.top, oldRect.right - newRect.left,
  330.                                                 oldRect.top - newRect.top, PATINVERT);
  331.                 PatBlt(hDC, newRect.left, newRect.bottom, oldRect.right - newRect.left,
  332.                                                 oldRect.bottom - newRect.bottom, PATINVERT);
  333.                 PatBlt(hDC, oldRect.right, newRect.top, newRect.right - oldRect.right,
  334.                                                 newRect.bottom - newRect.top, PATINVERT);
  335.                 PatBlt(hDC, oldRect.left, oldRect.top, newRect.left - oldRect.left,
  336.                                                 oldRect.bottom - oldRect.top, PATINVERT);
  337.             }
  338.             else if (oldRect.left > newRect.left || oldRect.right > newRect.right) {
  339.                 PatBlt(hDC, oldRect.left, newRect.top, newRect.right - oldRect.left,
  340.                                                 oldRect.top - newRect.top, PATINVERT);
  341.                 PatBlt(hDC, oldRect.left, newRect.bottom, newRect.right - oldRect.left,
  342.                                                 oldRect.bottom - newRect.bottom, PATINVERT);
  343.                 PatBlt(hDC, newRect.right, oldRect.top, oldRect.right - newRect.right,
  344.                                                 oldRect.bottom - oldRect.top, PATINVERT);
  345.                 PatBlt(hDC, newRect.left, newRect.top, oldRect.left - newRect.left,
  346.                                                 newRect.bottom - newRect.top, PATINVERT);
  347.             }
  348.         }
  349.         else if (oldRect.top < newRect.top || oldRect.bottom < newRect.bottom) {
  350.             if (oldRect.left <= newRect.left || oldRect.right <= newRect.right) {
  351.                 PatBlt(hDC, newRect.left, oldRect.top, oldRect.right - newRect.left,
  352.                                                 newRect.top - oldRect.top, PATINVERT);
  353.                 PatBlt(hDC, newRect.left, oldRect.bottom, oldRect.right - newRect.left,
  354.                                                 newRect.bottom - oldRect.bottom, PATINVERT);
  355.                 PatBlt(hDC, oldRect.right, newRect.top, newRect.right - oldRect.right,
  356.                                                 newRect.bottom - newRect.top, PATINVERT);
  357.                 PatBlt(hDC, oldRect.left, oldRect.top, newRect.left - oldRect.left,
  358.                                                 oldRect.bottom - oldRect.top, PATINVERT);
  359.             }
  360.             else if (oldRect.left > newRect.left || oldRect.right > newRect.right) {
  361.                 PatBlt(hDC, oldRect.left, oldRect.top, newRect.right - oldRect.left,
  362.                                                 newRect.top - oldRect.top, PATINVERT);
  363.                 PatBlt(hDC, oldRect.left, oldRect.bottom, newRect.right - oldRect.left,
  364.                                                 newRect.bottom - oldRect.bottom, PATINVERT);
  365.                 PatBlt(hDC, newRect.right, oldRect.top, oldRect.right - newRect.right,
  366.                                                 oldRect.bottom - oldRect.top, PATINVERT);
  367.                 PatBlt(hDC, newRect.left, newRect.top, oldRect.left - newRect.left,
  368.                                                 newRect.bottom - newRect.top, PATINVERT);
  369.             }
  370.         }
  371.     }
  372.     else {
  373.         PatBlt(hDC, oldRect.left, oldRect.top, oldRect.right - oldRect.left,
  374.                                                 oldRect.bottom - oldRect.top, PATINVERT);
  375.         PatBlt(hDC, newRect.left, newRect.top, newRect.right - newRect.left,
  376.                                                 newRect.bottom - newRect.top, PATINVERT);
  377.     }
  378. }
  379.  
  380.  
  381.